﻿Namespace Catalog
    Public Class Track
        Inherits BaseObject
        Public Property Title As String
        Public Property TrackNumber As Integer
        Public Property MixName As String
        Public Property ISRC As String
        Public Property ReleaseDate As Date
        Public Property PublishDate As Date
        Public Property SampleUrl As Uri
        Public Property Price As Price
        Public Property Length As TimeSpan
        Public Property BPM As Integer
        Public Property Artists As List(Of Artist)
        Public Property Genres As List(Of Genre)
        Public Property [Release] As Release
        Public Property [Label] As Label
        Public Property Images As List(Of Image)

        Public Sub New()
            Me.Artists = New List(Of Artist)
            Me.Genres = New List(Of Genre)
            Me.Images = New List(Of Image)
        End Sub

        Public Shared Function FromBPObject(tra As BPOTrack) As Track
            If tra Is Nothing Then Return Nothing
            Dim newTrack As New Track()

            With newTrack
                .ID = tra.id
                .Name = tra.name
                .Slug = tra.slug
                .Title = tra.title
                .TrackNumber = tra.trackNumber
                .MixName = tra.mixName
                .ISRC = tra.isrc
                Date.TryParseExact(tra.releaseDate, "yyyy-MM-dd", Nothing, Globalization.DateTimeStyles.None, .ReleaseDate)
                Date.TryParseExact(tra.publishDate, "yyyy-MM-dd", Nothing, Globalization.DateTimeStyles.None, .PublishDate)
                .SampleUrl = New Uri(tra.sampleUrl)
                .Price = Price.FromBPObject(tra.price)
                TimeSpan.TryParseExact(tra.length, "m:ss", Nothing, .Length)
                .BPM = tra.bpm

                For Each a As BPOBase In tra.artists
                    .Artists.Add(New Artist() With {.ID = a.id, .Name = a.name, .Slug = a.slug})
                Next

                For Each g As BPOGenreBase In tra.genres
                    Dim newGen As New Genre() With {.ID = g.id, .Name = g.name, .Slug = g.slug}

                    For Each sg As BPOGenreBase In tra.subGenres
                        newGen.Subgenres.Add(New BaseObject() With {.ID = sg.id, .Name = sg.name, .Slug = sg.slug})
                    Next

                    .Genres.Add(newGen)
                Next

                .Release = New Release() With {.ID = tra.release.id, .Name = tra.release.name, .Slug = tra.release.slug}
                .Label = New Label() With {.ID = tra.label.id, .Name = tra.label.name, .Slug = tra.label.slug}

                For Each i As KeyValuePair(Of String, BPOImage) In tra.images
                    .Images.Add(Image.FromBPObject(i))
                Next
            End With

            Return newTrack
        End Function

        Public Overrides Function ToString() As String
            Dim arts As New List(Of String)

            For Each a In Me.Artists
                arts.Add(a.Name)
            Next

            Return String.Format("{0} - {1}, {2}", String.Join(",", arts.ToArray), Me.Name, Me.ID)
        End Function
    End Class
End Namespace